﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace HyperPin_Database_Editor
{
    public partial class HPPinballWizardSettings : HPSettingsPanel
    {
        static string iniStart = "Start";
        static string iniExit = "Exit";
        static string iniUp = "Up";
        static string iniDown = "Down";
        static string iniSkipUp = "SkipUp";
        static string iniSkipDown = "SkipDown";
        static string iniFlyer = "Flyer";
        static string iniInstruction = "Instruction"; // TODO: Is this a typo in HyperPin or just the settings file?
        static string iniGenre = "Genre";
        static String iniService = "Service";

        public HPPinballWizardSettings()
        {
            InitializeComponent();

            // Populate the combo boxes with all the key names.
            for (int buttonNumber = 0; buttonNumber < 32; buttonNumber++)
            {
                string keyName = buttonNumberToName(buttonNumber);
                comboStart.Items.Add(keyName);
                comboExit.Items.Add(keyName);
                comboUp.Items.Add(keyName);
                comboDown.Items.Add(keyName);
                comboSkipUp.Items.Add(keyName);
                comboSkipDown.Items.Add(keyName);
                comboFlyer.Items.Add(keyName);
                comboInstructions.Items.Add(keyName);
                comboGenre.Items.Add(keyName);
                comboService.Items.Add(keyName);
            }
        }
 
        public override void doLoadSettings()
        {
            comboStart.Text = readSettingButton(iniStart, 9);
            comboExit.Text = readSettingButton(iniExit, 8);
            comboUp.Text = readSettingButton(iniUp, 2);
            comboDown.Text = readSettingButton(iniDown, 10);
            comboSkipUp.Text = readSettingButton(iniSkipUp, 3);
            comboSkipDown.Text = readSettingButton(iniSkipDown, 11);
            comboFlyer.Text = readSettingButton(iniFlyer, 4);
            comboInstructions.Text = readSettingButton(iniInstruction, 5);
            comboGenre.Text = readSettingButton(iniGenre, 7);
            comboService.Text = readSettingButton(iniService, 15);
        }

        public override void doSaveSettings()
        {
            writeSettingButton(iniStart, comboStart.Text);
            writeSettingButton(iniExit, comboExit.Text);
            writeSettingButton(iniUp, comboUp.Text);
            writeSettingButton(iniDown, comboDown.Text);
            writeSettingButton(iniSkipUp, comboSkipUp.Text);
            writeSettingButton(iniSkipDown, comboSkipDown.Text);
            writeSettingButton(iniFlyer, comboFlyer.Text);
            writeSettingButton(iniInstruction, comboInstructions.Text);
            writeSettingButton(iniGenre, comboGenre.Text);
            writeSettingButton(iniService, comboService.Text);
        }

        public string readSettingButton(string key, int defaultButton)
        {
            return buttonNumberToName(readSettingInt(key, defaultButton));
        }

        public void writeSettingButton(string key, string buttonName)
        {
            writeSettingInt(key, buttonNameToNumber(buttonName));
        }

        public string buttonNumberToName(int buttonNumber)
        {
            return string.Format("Button {0}", buttonNumber);
        }

        public int buttonNameToNumber(string buttonName)
        {
            Match buttonNumberMatch = Regex.Match(buttonName, "[0-9]{1,2}");
            if (buttonNumberMatch.Success)
            {
                return (int)Int32.Parse(buttonNumberMatch.Value);
            }

            return 0;
        }
    }
}
